
import { IObject, IViewHooks, IViewHooksOptions } from "./interface";
import baseService from "./baseService";
import { ElMessage, ElMessageBox } from "element-plus";
import { onMounted } from "vue";

/**
 * 通用视图业务逻辑（列表/增删改查基本业务） 
 * @param props 自定义通用业务state
 * @returns 返回响应式自定义state和通用方法
 */
const listService = (props: IViewHooksOptions | IObject): IViewHooks => {

    const defaultOptions: IViewHooksOptions = {
        createdIsNeed: true,
        activatedIsNeed: false,
        getDataListURL: "",
        getDataListIsPage: false,
        deleteURL: "",
        deleteIsBatch: false,
        deleteIsBatchKey: "id",
        exportURL: "",
        dataForm: {},
        dataList: [],
        order: "",
        orderField: "",
        page: 1,
        limit: 10,
        total: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        addOrUpdate: {},
        elTable: {},
        refreshData: false
    };
    const mergeDefaultStateToPageState = (options: IObject, props: IObject): IViewHooksOptions => {
        for (const key in options) {
            if (!Object.getOwnPropertyDescriptor(props, key)) {
                props[key] = options[key];
            }
        }
        console.log(props);
        return props;
    };
    const state = mergeDefaultStateToPageState(defaultOptions, props);
    onMounted(() => {
       
        if (state.createdIsNeed || !state.activatedIsNeed) {
           viewFns.query();
        }
    });
    const viewFns = {
        // 获取数据列表 
        query() {
            if (!state.getDataListURL) {
                return;
            }
            if (state.page != 1 && !state.refreshData) {
                return;
            }
            if (state.page == 1) {
                state.dataList = []
            }
            state.dataListLoading = true;
            baseService
                .get(state.getDataListURL, {
                    order: state.order,
                    orderField: state.orderField,
                    page: state.getDataListIsPage ? state.page : null,
                    pageNumber: state.getDataListIsPage ? state.page : null,
                    limit: state.getDataListIsPage ? state.limit : null,
                    ...state.dataForm
                })
                .then((res) => {
                    state.dataListLoading = false;
                    if (res.code !== 0) {
                        state.dataList = [];
                        state.total = 0;
                        return ElMessage.error(res.msg)
                    }
                    let data = state.getDataListIsPage ? res.data.list : res.data;
                    state.refreshData = true;
                    if (data.length == 0) {
                        state.refreshData = false;
                    }
                    state.dataList = state.dataList?.concat(data);
                    state.total = state.getDataListIsPage ? res.data.total : 0;
                })
                .catch(() => {
                    state.dataListLoading = false;
                });
        },
        // 分页, 每页条数
        pageSizeChangeHandle(val: number) {
            state.page = 1;
            state.limit = val;
            viewFns.query();
        },
        // 分页, 当前页
        pageCurrentChangeHandle(val: number) {
            state.page = val;
            viewFns.query();
        },
        //搜索
        getDataList() {
            state.page = 1;
            viewFns.query();
        },
        reset() {
            if (state.dataForm) {
                for (const itemKey in state.dataForm) {
                    state.dataForm[itemKey] = '';
                }

            }
        }

    };


    //
    return {
        ...viewFns
    };

};
export default listService;

